import "./detail.scss";
import $ from "jquery";
import Cart from "../../parts/cart/cart";
import ProfileMgr from "../../data/ProfileMgr";
import MidWine from "./midWine";
import ProWine from "./proWine";

class Detail{
    constructor(){

    }
    init(goodsId){
        this.m_goodsId = goodsId;
    }
    getDetailData(callback){
        let data = ProfileMgr.getDetailById(this.m_goodsId);
        new MidWine(data, ".mid_wine",callback);
        new ProWine({
            rootCls: ".wine_pro .miaoshu",
            data: data.detail
        })
    }
}

let goodsDetail = new Detail();
$(function(){
    // 是否读取一遍本地数据
    ProfileMgr.repeatLoad();
    $('.web_head').load('./header.html',function(){
        // 购物车
        let myCart = new Cart();
        let goodsDetails = ProfileMgr.getGoodsDetails();
        if(!goodsDetails || goodsDetails.length <= 0){
            $.ajax("./server/goods/detail.json",{
                dataType: "json"
            })
            .then(res=>{
                goodsDetails = res.data;
                ProfileMgr.setGoodsDetails(goodsDetails);
                goodsDetail.getDetailData(myCart.addCart.bind(myCart));
            });
        }
        else{
            goodsDetail.getDetailData(myCart.addCart.bind(myCart));
        }
    });
    $('.footer').load('./footer.html');
});
module.exports = goodsDetail;